﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_MenuHTML.Business
{
    public class DBMenuHTML
    {
        private readonly MenuHTMLDC db = new MenuHTMLDC();

        public tbl_MenuHTML GetOne(Guid itemGuid)
        {
            var result = from p in db.tbl_MenuHTMLs
                         where p.Guid.Equals(itemGuid)
                         select p;
            return result.FirstOrDefault();
        }

        public List<tbl_MenuHTML> GetAll(int moduleID)
        {
            var result = from p in db.tbl_MenuHTMLs
                         where p.ModuleID.Equals(moduleID)
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p;
            return result.ToList();
        }

        public bool Delete(Guid itemGuid)
        {
            bool flag = false;
            tbl_MenuHTML item = GetOne(itemGuid);
            db.tbl_MenuHTMLs.DeleteOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Create(int moduleID, string name, string link, string target, int sortOrder, Guid parentGuid, string itemUrl)
        {
            bool flag = false;

            tbl_MenuHTML item = new tbl_MenuHTML
                                    {
                                        Guid = Guid.NewGuid(),
                                        ModuleID = moduleID,
                                        Name = name,
                                        Link = link,
                                        Target = target,
                                        ParentGuid = parentGuid,
                                        SortOrder = sortOrder,
                                        ImageUrl = itemUrl,
                                        LastModified = DateTime.UtcNow
                                    };
            db.tbl_MenuHTMLs.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid itemGuid, string name, string link, string target, int sortOrder, Guid parentGuid, string itemUrl)
        {
            bool flag = false;

            tbl_MenuHTML item = GetOne(itemGuid);
            if (item != null)
            {
                item.Name = name;
                item.Link = link;
                item.Target = target;
                item.ParentGuid = parentGuid;
                item.SortOrder = sortOrder;
                item.ImageUrl = itemUrl;
                item.LastModified = DateTime.UtcNow;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            var result = from p in db.tbl_MenuHTMLs
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.tbl_MenuHTMLs.DeleteAllOnSubmit(result);
            db.SubmitChanges();
        }
    }
}
